﻿using System.Windows;
using System.Windows.Controls;

using ICSharpCode.AvalonEdit.Highlighting.Xshd;
using ICSharpCode.AvalonEdit.Highlighting;
using System.Xml;
using Xc.Views.Booster.Services;

namespace Xc.Views.Booster.Views
{
    /// <summary>
    /// 系统参数配置页面
    /// </summary>
    public partial class SettingView : UserControl
    {
        private readonly IAppSettingService appSettingService;
        public SettingView(IAppSettingService appSettingService)
        {
            InitializeComponent();
            this.appSettingService = appSettingService;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            _textEditor.Text = appSettingService.GetAppSetting();
            _textEditor.SyntaxHighlighting=LoadHighlightingDefinition("ICSharpCode.AvalonEdit.Highlighting.Resources.Json.xshd");

        }
        public  IHighlightingDefinition LoadHighlightingDefinition(string resourceName)
        {

            var assembly = typeof(HighlightingLoader).Assembly;
            using (System.IO.Stream s = assembly.GetManifestResourceStream(resourceName))
            {
                using (XmlTextReader reader = new XmlTextReader(s))
                {
                    var xshd = HighlightingLoader.LoadXshd(reader);
                    return HighlightingLoader.Load(xshd, HighlightingManager.Instance);
                }
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var res = appSettingService.SetAppSetting(_textEditor.Text);
            if (res.IsSuccess)
            {
                MessageBox.Show(res.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Information); 
            }
            else
            {
                MessageBox.Show(res.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);

            }
        }
    }
}
